package jdk8_new_features;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class LocalDatePractice {
	
	public static void main(String[] args) {
		LocalDate today = LocalDate.now();
		System.out.println("日期：today = " + today);
		int year = today.getYear();
		System.out.println("getYear= " + year );
		int month = today.getMonthValue();
		System.out.println("getMonthValue= " + month + "-----getMonth = " + today.getMonth() +"----lengthOfMonth="+today.lengthOfMonth());
		int day = today.getDayOfMonth();
		System.out.println("getDayOfMonth= " + day + "------getDayOfYear="+today.getDayOfYear() +"-----getDayOfWeek="+today.getDayOfWeek());

		LocalDate last12Month = today.minusMonths(12);
		System.out.println(last12Month.toString() +"getMonthValue"+last12Month.getMonthValue());
		System.out.println("----------------------"+ last12Month.format(DateTimeFormatter.ofPattern("yyyy年MM月")));
		
		LocalTime now = LocalTime.now();
		System.out.println("时间：now = "  + now);
		
		LocalDateTime dateTime = LocalDateTime.now();
		System.out.println("现在的时间：dateTime= " + dateTime);
		
		Instant instant = Instant.now();
		System.out.println("时间戳 =" + instant);

       //获取当前的时间，包括毫秒
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println("当前年:"+ldt.getYear());   //2018
		System.out.println("当前年份天数:"+ldt.getDayOfYear());//172
		System.out.println("当前月:"+ldt.getMonthValue());
		System.out.println("当前时:"+ldt.getHour());
		System.out.println("当前分:"+ldt.getMinute());
		System.out.println("当前时间:"+ldt.toString());


		//格式化时间
		System.out.println("格式化时间: "+ ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
        //格式化时间:2018-12-19 15:37:47.119


		LocalDateTime ldt2 = LocalDateTime.now();
		System.out.println("后5天时间:"+ldt.plusDays(5));
		System.out.println("前5天时间并格式化:"+ldt.minusDays(5).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); //2018-06-16
		System.out.println("前一个月的时间:"+ldt2.minusMonths(1).format(DateTimeFormatter.ofPattern("yyyyMM"))); //2018-06-16
		System.out.println("后一个月的时间:"+ldt2.plusMonths(1)); //2018-06-16
		System.out.println("指定2099年的当前时间:"+ldt.withYear(2099)); //2099-06-21T15:07:39.506
		//  后5天时间:2018-12-24T15:50:37.508
		//  前5天时间并格式化:2018-12-14
		//  前一个月的时间:201712
		//  后一个月的时间:2018-02-04T09:19:29.499
		//  指定2099年的当前时间:2099-12-19T15:50:37.508

		//通过指定年月日来创建
		LocalDate ld3=LocalDate.of(2017, Month.NOVEMBER, 17);
		LocalDate ld4=LocalDate.of(2018, 02, 11);


		//时间相差比较
		LocalDate ld=LocalDate.parse("2017-11-17");
		LocalDate ld2=LocalDate.parse("2018-01-05");
		Period p=Period.between(ld, ld2);
		System.out.println("相差年: "+p.getYears()+" 相差月 :"+p.getMonths() +" 相差天:"+p.getDays());
		// 相差年: 0 相差月 :1 相差天:19

		//ChronoUnit 日期周期单位的标准集合
		LocalDate startDate = LocalDate.of(2017, 11, 17);
		LocalDate endDate = LocalDate.of(2018, 01, 05);
		System.out.println("相差月份:"+ ChronoUnit.MONTHS.between(startDate, endDate));
		System.out.println("两月之间的相差的天数   : " + ChronoUnit.DAYS.between(startDate, endDate));
		//        相差月份:1
		//        两天之间的差在天数   : 49



		//Duration 这个类以秒和纳秒为单位建模时间的数量或数量。
		Instant inst1 = Instant.now();
		System.out.println("当前时间戳 : " + inst1);
		Instant inst2 = inst1.plus(Duration.ofSeconds(10));
		System.out.println("增加之后的时间 : " + inst2);
		System.out.println("相差毫秒 : " + Duration.between(inst1, inst2).toMillis());
		System.out.println("相毫秒 : " + Duration.between(inst1, inst2).getSeconds());
		// 当前时间戳 : 2018-12-19T08:14:21.675Z
		// 增加之后的时间 : 2018-12-19T08:14:31.675Z
		// 相差毫秒 : 10000
		// 相毫秒 : 10






	}

	public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
		Instant instant = Instant.ofEpochMilli(timestamp);
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zone);
	}


	public static LocalDateTime parseStringToDateTime(String time) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return LocalDateTime.parse(time, df);
	}

}
